package cn.lmxdev.ptms.controller;

import cn.lmxdev.ptms.bean.table.TDepartment;
import cn.lmxdev.ptms.bean.table.TUser;
import cn.lmxdev.ptms.bean.vo.ResponseVo;
import cn.lmxdev.ptms.service.IDepartmentServlet;
import cn.lmxdev.ptms.service.IUserServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController(value = "departmentController")
@RequestMapping(value = "/api/department")
public class DepartmentController {
    private final IDepartmentServlet departmentServlet;
    private final IUserServlet userServlet;

    @Autowired
    public DepartmentController(
            IDepartmentServlet departmentServlet,
            IUserServlet userServlet
    ) {
        this.departmentServlet = departmentServlet;
        this.userServlet = userServlet;
    }

    @GetMapping(value = "/list")
    public ResponseVo<Object> list(
            @RequestParam(value = "limit", required = false) Long limit,
            @RequestParam(value = "page", required = false) Long page,
            TDepartment department
    ) {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        TUser nowUser = this.userServlet.getByAccount(auth.getName());

        // 响应数据变量
        long count = 0L;
        Object data = null;

        // 校验角色
        switch (auth.getAuthorities().toString()) {
            case "[ROLE_ADMIN]" -> {
                count = this.departmentServlet.count(department);
                data = this.departmentServlet.list(limit, page, department);
            }
            case "[ROLE_DEPARTMENT-HEAD]" -> {
                count = this.departmentServlet.count(department.setDepartmentHeadId(nowUser.getId()));
                data = this.departmentServlet.list(limit, page, department.setDepartmentHeadId(nowUser.getId()));
            }
            case "[ROLE_SPECIALTY-HEAD]" -> {
            }
            case "[ROLE_ADVISER]" -> {
            }
            case "[ROLE_TEACHER]" -> {
            }
            case "[ROLE_STUDENT]" -> {
            }
        }

        // 响应数据
        return new ResponseVo<>(count, data);
    }

    @PostMapping(value = "/add")
    public ResponseVo<Object> add(TDepartment department) {
        this.departmentServlet.add(department);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @DeleteMapping(value = "/delete/{id}")
    public ResponseVo<Object> delete(@PathVariable(value = "id") Long id) {
        this.departmentServlet.remove(id);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @PutMapping(value = "/edit")
    public ResponseVo<Object> edit(TDepartment department) {
        this.departmentServlet.edit(department);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @GetMapping(value = "/look/{id}")
    public ResponseVo<Object> look(@PathVariable(value = "id") Long id) {
        return new ResponseVo<>(this.departmentServlet.get(id));
    }
}
